//
//  BudgetEditeVC.m
//  BaseProject
//
//  Created by mac on 2020/10/26.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BudgetEditeVC.h"
#import "BudgetEditeSectionView.h"
@interface BudgetEditeVC ()
@property(nonatomic,strong)UIScrollView *mainView;
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)NSMutableArray<BudgetEditeSectionView *> *itemsArray;
@property(nonatomic,strong)UIButton *sureButton;
@end

@implementation BudgetEditeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createProperty];
    [self createUI];
}
- (void)dealloc
{
    NSLog(@"BudgetEditeVC ---dealloc");
}
- (void)createProperty
{
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)createUI
{
    self.title = @"婚礼预算表";
    [self titleLab];
    WEAKSELF
    CGFloat orinY = 0;
    for (NSInteger index = 0; index < self.dataSource.count; index ++) {
        BudgetEditeSectionView *view = [[BudgetEditeSectionView alloc]initWithFrame:CGRectMake(0, orinY, TTScreenWidth, 0)];
        [self.mainView addSubview:view];
        view.iconModel = self.dataSource[index];
        view.dataSource = self.dataSource;
        
        orinY = view.bottom;
        [self.itemsArray addObject:view];
        
        [view setRealTimeCallBackBlock:^{
            [weakSelf realTimeCallBack];
        }];
    }
    self.mainView.contentSize = CGSizeMake(0, MAX(self.mainView.height + 1, orinY + kBottomSafeHeight + 20*ADAPTER_WIDTH));
    [self sureButton];
}
#pragma mark - realTime
- (void)realTimeCallBack
{
    NSInteger hasNum = 0;
    for (NSInteger index = 0; index < self.dataSource.count; index ++) {
        BudgetPieItemModel *indexModel = self.dataSource[index];
        for (NSInteger flag = 0; flag < indexModel.dataSource.count; flag ++) {
            BudgetPieItemModel *flagModel = indexModel.dataSource[flag];
            if (flagModel.isSelected) {
                hasNum = hasNum + 1;
            }
        }
    }
    if (hasNum == 0) {
        self.sureButton.backgroundColor = RGB(220, 220, 220);
        self.sureButton.userInteractionEnabled = NO;
    } else {
        self.sureButton.backgroundColor = RGB(250, 83, 76);
        self.sureButton.userInteractionEnabled = YES;
    }
}
#pragma mark - click
- (void)clickSureButton:sender
{
    !self.sureBlock ?: self.sureBlock ();
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - lazy懒加载
- (UIScrollView *)mainView
{
    if (!_mainView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.titleLab.bottom + 25*ADAPTER_WIDTH, TTScreenWidth, TTScreenHeight - kNavBarAndStatusBarHeight - kBottomSafeHeight - 44*ADAPTER_WIDTH - 10*ADAPTER_WIDTH - self.titleLab.bottom - 25*ADAPTER_WIDTH)];
        [self.view addSubview:view];
        view.backgroundColor = RGB(255, 255, 255);
        view.showsVerticalScrollIndicator = NO;
        _mainView = view;
    }
    return _mainView;
}
- (NSMutableArray<BudgetEditeSectionView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.view addSubview:lab];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(16*ADAPTER_WIDTH, 22*ADAPTER_WIDTH, TTScreenWidth - 32*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
        
        NSMutableAttributedString *titleAttributes = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"选择预算项目"]];
        titleAttributes.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightBold];
        titleAttributes.color = RGB(51, 51, 51);
        
        NSMutableAttributedString *titleAttributes1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",@"(多选)"]];
        titleAttributes1.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightBold];
        titleAttributes1.color = RGB(190, 190, 190);
        
        [titleAttributes appendAttributedString:titleAttributes1];
        lab.attributedText = titleAttributes;
        _titleLab = lab;
    }
    return _titleLab;
}
- (UIButton *)sureButton
{
    if (!_sureButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:button];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:18*ADAPTER_WIDTH weight:UIFontWeightBold];
        [button addTarget:self action:@selector(clickSureButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, TTScreenHeight - kNavBarAndStatusBarHeight - kBottomSafeHeight - 44*ADAPTER_WIDTH, TTScreenWidth, 44*ADAPTER_WIDTH);
        button.backgroundColor = RGB(250, 83, 76);
        _sureButton = button;
    }
    return _sureButton;
}
@end
